#include <my_header.h>

void* threadFunc(void* args)
{
    int socfd = *((int*)args);
    char readBuf[1024];
    while(1)
    {
        memset(readBuf,0,strlen(readBuf));
        int ret = read(socfd,readBuf,1024);
        if(ret==0)
        {
            printf("server closed\n");
            break;
        }
        printf("%s\n",readBuf);
        usleep(100);
    }
    return NULL;
}
int main(int argc, char *argv[])
{
    int namestrlen = strlen(argv[1]);
    int socfd = socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(socfd,-1,"socket");
    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(12345);
    addr.sin_addr.s_addr =inet_addr("192.168.8.96");

    int ret = connect(socfd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");

    char writeBuf[1024];
    pthread_t readthread;

    pthread_create(&readthread,NULL,threadFunc,(void*)&socfd);

    while(1)
    {            
        memset(writeBuf,0,strlen(writeBuf));
        memcpy(writeBuf,argv[1],namestrlen);
        writeBuf[namestrlen]=':';
        writeBuf[namestrlen+1]=' ';
        scanf("%s",writeBuf+namestrlen+2);
        write(socfd,writeBuf,strlen(writeBuf));
        usleep(100);
    }
 
    // usleep(100);
    pthread_join(readthread,NULL);
    close(socfd);
    return 0;
}
